<template>
	<view v-if="giftInfo">
		<my-answer-alert :show="answerShow"  @close="answerShow=false" :giftInfo="giftInfo" @onSubmitAnswer="onSubmitAnswer"></my-answer-alert>
		<my-answer-count :show="answerCountShow" :id="giftInfo.id" @close="answerCountShow=false"></my-answer-count>
		<my-answer-error :show="answerErrorShow" :errorNum="errorNum" @close="answerErrorShow=false" @ok="answerErrorShow=false,answerShow=true"></my-answer-error>
		<my-answer-ok :show="answerOkShow" @close="answerOkShow=false"></my-answer-ok>
	</view>
</template>

<script setup>
	import {subjectGiftInfo,joinGift} from "@/api/gift.js"
	import {ref} from "vue"
	const emits = defineEmits();
	const giftInfo=ref('')
	const answer=ref('')
	const answerOkShow=ref(false)
	const answerErrorShow=ref(false)
	const answerShow=ref(false)
	const answerCountShow=ref(false)
	const errorNum=ref(0)
	let props = defineProps({
			id:{
				type:Number,
				default:''
			}
		});
	//立即破解点击
	const onAnswer=async (info)=>{
		// let res=await subjectGiftInfo({params:{id:id}})
		// giftInfo.value=res.data.info
		giftInfo.value=info
		answer.value=''
		answerShow.value=true
	}
	
	const onSubmitAnswer=async(e)=>{
		if(e=='') return uni.$u.toast('答案不能为空！');
		try {
			let res=await joinGift({id:props.id,answer:e})
			answerShow.value=false;
			if(res.data.status==1){//破解成功！
				answerOkShow.value=true
				paging.value.refresh()
			}else{
				errorNum.value=res.data.has_num
				answerErrorShow.value=true
				// if(res.has_num==0){//破解次数已用完
				// 	answerCountShow.value=true
				// }else{
				// 	answerErrorShow.value=true
				// }
			}
		} catch (error) {
			//TODO handle the exception
			if(error.status==400){
				answerShow.value=false;
				answerCountShow.value=true
			}else if(error.msg){
				uni.$u.toast(error.msg)
			}
		}
		
	}
	defineExpose({ onAnswer });
</script>

<style lang="scss">
</style>